<?php

namespace app\common\model\banxi\course;

use app\common\model\common\BaseModel;
use think\exception\ValidateException;
use think\model\concern\SoftDelete;

/**
 * 教学材料
 */
class TeachingMaterial extends BaseModel
{
    use SoftDelete;

    protected $name = 'banxi_teaching_material';

    protected $autoWriteTimestamp = true;

    protected $deleteTime = 'del';

    protected $defaultSoftDelete = 0;

    protected $append = [];
    protected $type = [
        'id' => 'string',
        'del' => 'string',
        'createTime' => 'timestamp:Y-m-d H:i:s',
        'updateTime' => 'timestamp:Y-m-d H:i:s'
    ];

    //-----------------------------------------------------------------------------------
    //｜                                   获取器                                         ｜
    //-----------------------------------------------------------------------------------

    //-----------------------------------------------------------------------------------
    //｜                                   处理方法                                       ｜
    //-----------------------------------------------------------------------------------

    public function getListPage(array $where, int $page = 1, int $limit = 10)
    {
        return $this->where($where)->paginate([
            'page' => $page,
            'list_rows' => $limit
        ]);
    }

    public function saveOrUpdate($data)
    {
        $id = $data['id'] ?? null;
        if ($id) {
            $info = $this->where(['id' => $id])->find();
            if (!$info) {
                throw new ValidateException('记录不存在');
            }
            $info->save($data);
        } else {
            $this->save($data);
        }
        return true;
    }

    public function  del($ids)
    {
        return self::destroy(function ($query) use ($ids) {
            $query->whereIn('id', $ids);
        });
    }


    //-----------------------------------------------------------------------------------
    //｜                                   模型关联                                       ｜
    //-----------------------------------------------------------------------------------


}